import nodejieba  from "nodejieba";
export default class pattern_util{
  getTimePattern(searchTime){
    let date = new Date();
    let time_pattern = `${20}`;
    switch(searchTime){
      case "all":
        break;
      case "day":
        time_pattern = `${date.getFullYear().toString()}-${date.getMonth()+1}-${date.getDate()}`
        break;
      case "year":
        time_pattern =`${date.getFullYear().toString()}`
        break;
      case "month":
        time_pattern =`${date.getFullYear().toString()}-${date.getMonth()+1}`
        break;
      case "week":
        let day = date.getDate();
        let month = date.getMonth()+1;
        let year = date.getFullYear();
        let day_pattern = '('
        let month_pattern = `${month.toString()}-`
        let year_pattern =`${year.toString()}-`
        for (let i =0;i<7;i++){
          if(i!=6) {
            day_pattern += day.toString() + '|'
          }else{
            day_pattern += day.toString()+')';
          }
          day--;
          if(day==0){
            month--;
            if(month>0){
              if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
                day = 31;
              }else if(month==4||month==6||month==9||month==11){
                day = 30;
              }else{
                day = 28 + (day%4==0&&day%100==0?1:0);
              }
              year_pattern = `${year.toString()}-`
              month_pattern = `(${month.toString()}|${(month+1).toString()})-`
            }
            else{
              year--;
              month =12;
              day = 31;
              year_pattern = `${year.toString()}-`
              month_pattern = `(${month.toString()}|1)-`
            }
          }
        }
        time_pattern = year_pattern+month_pattern+day_pattern
        break;
      default:
        return "none";
    }
    return time_pattern;
  }
  getSearchCondition(searchContent,reg){
    let condition = []
    switch(searchContent){
      case "all":
        condition.push({title:reg})
        condition.push({material:reg})
        condition.push({qualifications: reg})
        break;
      case "title":
        condition.push({title: reg})
        break;
      case "content":
        condition.push({material: reg})
        condition.push({qualifications: reg})
        break;
      default:
        break;
    }
    return condition
  }
  getKeywordSegmentReg(keyword){
    let keys = nodejieba.cut(keyword);
    let pattern = ''
    for (let i =0;i<keys.length;i++){
      if(i==0){
        pattern = '('+keys[i]+'|';
      }
      if(i==keys.length-1){
        pattern += keys[i]+')';
      }
      if(i!=0&&i!=keys.length-1){
        pattern += keys[i]+'|';
      }
    }
    return pattern;
  }
}
